package com.wangxin.study8.util;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jojo.wang
 * @title: SortUtil
 * @projectName study8
 * @description: 排列组合
 * @date 2019/5/917:32
 */

public class SortUtil {
    public static void allSort(List<String[]> list, String[] array, int begin, int end) {
//    打印数组的内容
        if (begin == end) {
            String[] a = Arrays.copyOf(array, array.length);
            list.add(a);
        }
//    把子数组的第一个元素依次和第二个、第三个元素交换位置
        for (int i = begin; i <= end; i++) {
            swap(array, begin, i);
            allSort(list, array, begin + 1, end);
//        交换回来
            swap(array, begin, i);
        }
    }

    private static void swap(String[] array, int a, int b) {
        String tem = array[a];
        array[a] = array[b];
        array[b] = tem;
    }

    public static void main(String[] args) {
        List<String[]> result = new ArrayList<>();
        String orderNoArr[] ={"1","2","3","4"};
        SortUtil.allSort(result, orderNoArr, 0, orderNoArr.length - 1);
        System.out.println(JSON.toJSON(result));
    }
}
